package com.foodndate.food.coupon;

/*
 * 컨트롤러*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/coupon")
public class CouponController {
@Autowired private CouponDAO dao;
@Autowired private CouponService service;
	

//관리자 쿠폰등록 화면
@RequestMapping (value="/insert", method=RequestMethod.GET)
public String insertPage(){
	return "coupon/couponAdd";
}

//관리자 쿠폰등록

@RequestMapping (value="/insertProcess", method=RequestMethod.POST)
public String insert(CouponDTO dto,Model model,CouponListDTO cdto){
	
	service.insert(dto);
	
	CouponListDTO list=service.list(dto, cdto);
	model.addAttribute("listDTO", list);
	
	return "redirect:/coupon/list";
	
}

//관리자 쿠폰리스트	
@RequestMapping (value="/list", method=RequestMethod.GET)
public String list(CouponDTO dto,Model model,CouponListDTO cdto){
	CouponListDTO list=service.list(dto, cdto);
	model.addAttribute("listDTO", list);
	
	return "coupon/couponList";
}


//쿠폰상세보기
@RequestMapping(value="/detail", method= RequestMethod.GET)
public String detail(CouponDTO dto,Model model,CouponListDTO cdto){
	//String detail="쿠폰상세보기";
	model.addAttribute("dto", dao.detail(dto));
	
	CouponListDTO list=service.list(dto, cdto);
	model.addAttribute("listDTO", list);
		
	return "coupon/couponDetail";
}
/*/*상세보기에서 수정 및 목록 클릭시 현재페이지까지 같이 받아와서  보던 목록(페이지)로 넘어가도록 함*/


//쿠폰 수정페이지
@RequestMapping(value="/updatePage", method= RequestMethod.GET)
public String updatePage(CouponDTO dto,Model model,CouponListDTO cdto){
	//String update="쿠폰수정";
	model.addAttribute("dto", dao.detail(dto));     //수정을 위해 상세페이지정보를 가져옴
	
	CouponListDTO list=service.list(dto, cdto);
	model.addAttribute("listDTO", list);   //페이지번호를 같이 가져오기 위해 CouponListDTO를 불러옴
	
	return "coupon/couponUpdate";   
}


//쿠폰 수정
@RequestMapping(value="/update", method= RequestMethod.POST)
public String update(CouponDTO dto,Model model,CouponListDTO cdto) {	
	service.edit(dto);
	
	model.addAttribute("dto", dao.detail(dto));    //수정 후 수정된 내용을 다시 보여줌
	
	CouponListDTO list=service.list(dto, cdto);
	model.addAttribute("listDTO", list);      /* #페이지 수정후 목록보기시 #페이지로 넘기기 완료*/
	return "coupon/couponDetail";
	
	
	
	/*CouponListDTO list=service.list(dto, cdto);
	model.addAttribute("listDTO", list);
	return "coupon/couponDetail";*///수정후 바로 리스트로 보여줌
}


//쿠폰삭제
@RequestMapping(value="/delete", method= RequestMethod.GET)
public String delete(CouponDTO dto,Model model,CouponListDTO cdto){
	dao.delete(dto);
		
	CouponListDTO list=service.list(dto, cdto);
	model.addAttribute("listDTO", list);
	return "coupon/couponList";
	
	
}

//쿠폰메인리스트(전체)
@RequestMapping (value="/home/list", method=RequestMethod.GET)
public String list1(CouponDTO dto, Model model,CouponListDTO cdto){
	CouponListDTO home=service.list(dto, cdto);
	model.addAttribute("listDTO", home);
	model.addAttribute("list", "list");
	
	return "coupon/couponHome";
}
//쿠폰메인리스트(오늘까지)
@RequestMapping (value="/home/today", method=RequestMethod.GET)
public String list_today(CouponDTO dto, Model model,CouponListDTO cdto){
	CouponListDTO home=service.list_today(dto, cdto);
	model.addAttribute("listDTO", home);
	model.addAttribute("today", "today");
	
	return "coupon/couponHome";
}
//쿠폰메인리스트(할인률)
@RequestMapping (value="/home/discount", method=RequestMethod.GET)
public String list_discount(CouponDTO dto, Model model,CouponListDTO cdto){
	CouponListDTO home=service.list_discount(dto, cdto);
	model.addAttribute("listDTO", home);
	model.addAttribute("discount", "discount");
	
	return "coupon/couponHome";
}
//쿠폰메인리스트(조회수)
@RequestMapping (value="/home/readcnt", method=RequestMethod.GET)
public String list_readcnt(CouponDTO dto, Model model,CouponListDTO cdto){
	CouponListDTO home=service.list_readcnt(dto, cdto);
	model.addAttribute("listDTO", home);
	model.addAttribute("readcnt", "readcnt");
	
	return "coupon/couponHome";
}

//쿠폰출력용
@RequestMapping(value="/view", method= RequestMethod.GET)
public String view(CouponDTO dto,Model model){
	
	model.addAttribute("dto", dao.detail(dto));
	return "coupon/couponView";
}







}
